﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Sales;

namespace ESOL.WinUI.Sales
{
    public partial class frmManagePersonalWorkPlan : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private DateTime mSelectedDate = Convert.ToDateTime("1900-01-01");
        private List<SalesUserSchedulerEntity> mDeleteList;
        #endregion

        #region Constructor
        public frmManagePersonalWorkPlan()
        {
            InitializeComponent();
        }
        public frmManagePersonalWorkPlan(MstPersonEntity person, DateTime date)
        {
            InitializeComponent();
            mSelectedPerson = person;
            mSelectedDate = date;
        }
        #endregion

        #region Functions
        private void setField()
        {
            txtPerson.Text = mSelectedPerson.NameCn;
            txtPerson.Tag = mSelectedPerson;
        }
        private void bindScheduler()
        {
            DataTable dt = new DataTable();

            dt = SalesUserSchedulerBs.GetSalesUserSchedulerByPersonId(mSelectedPerson.PersonId).Tables[0];
            if (dt == null || dt.Rows.Count == 0)
            {

            }
            scheControl.Storage = schedulerStorage1;
            schedulerStorage1.Appointments.DataSource = dt;
            schedulerStorage1.Appointments.AutoReload = true;
            schedulerStorage1.Appointments.Mappings.AllDay = "APP_ALL_DAY";
            schedulerStorage1.Appointments.Mappings.Description = "APP_DESCRIPTION";
            schedulerStorage1.Appointments.Mappings.End = "APP_END";
            schedulerStorage1.Appointments.Mappings.Label = "APP_LABEL";
            schedulerStorage1.Appointments.Mappings.Location = "APP_LOCATION";
            schedulerStorage1.Appointments.Mappings.RecurrenceInfo = "APP_RECURRENCE_INFO";
            schedulerStorage1.Appointments.Mappings.ReminderInfo = "APP_REMINDER_INFO";
            schedulerStorage1.Appointments.Mappings.ResourceId = "APP_RESOURCE_ID";
            schedulerStorage1.Appointments.Mappings.Start = "APP_START";
            schedulerStorage1.Appointments.Mappings.Status = "APP_STATUS";
            schedulerStorage1.Appointments.Mappings.Subject = "APP_SUBJECT";
            schedulerStorage1.Appointments.Mappings.Type = "APP_TYPE";
        }

        private void ResetDateTimePicker()
        {
            if (dtpStartTime.Text == "")
                return;
            this.dtpStartTime.EditValueChanged -= new System.EventHandler(this.dtpStartTime_EditValueChanged);
            while (Convert.ToDateTime(dtpStartTime.EditValue).DayOfWeek != DayOfWeek.Monday)
            {
                dtpStartTime.EditValue = Convert.ToDateTime(dtpStartTime.EditValue).AddDays(-1);
            }
            this.dtpStartTime.EditValueChanged += new System.EventHandler(this.dtpStartTime_EditValueChanged);
        }

        #endregion

        private void frmManagePersonalWorkPlan_Load(object sender, EventArgs e)
        {
            dtpStartTime.EditValue = mSelectedDate;
            ResetDateTimePicker();
            setField();
            bindScheduler();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            schedulerStorage1.Appointments.EndUpdate();
            
            DataTable dt = schedulerStorage1.Appointments.DataSource as DataTable;
            DataTable dtInsert = dt.Copy();
            dtInsert.Clear();
            
            foreach (DataRow dr in dt.Rows)
            {
                //if (dr.RowState == DataRowState.Deleted)
                //{
                //    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                //    bool isSucessful=SalesUserSchedulerBs.DeleteSalesUserScheduler(entity);
                //    if (!isSucessful)
                //    {
                //        MessageBox.Show("删除工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //        return;
                //    }
                //}
                if (string.IsNullOrEmpty(dr["SCHEDULER_ID"].ToString()))
                {
                    dr["SCHEDULER_ID"] = Guid.NewGuid();
                    dr["PERSON_ID"] = mSelectedPerson.PersonId;
                    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                    bool isSucessful = SalesUserSchedulerBs.InsertSalesUserScheduler(entity);
                    if (!isSucessful)
                    {
                        MessageBox.Show("新增工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                    bool isSucessful = SalesUserSchedulerBs.UpdateSalesUserScheduler(entity);
                    if (!isSucessful)
                    {
                        MessageBox.Show("修改工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            bindScheduler();
            
        }

        private void dtpStartTime_EditValueChanged(object sender, EventArgs e)
        {
            ResetDateTimePicker();
            scheControl.Start = Convert.ToDateTime(dtpStartTime.EditValue);
        }

        private void schedulerStorage1_AppointmentDeleting(object sender, DevExpress.XtraScheduler.PersistentObjectCancelEventArgs e)
        {
            if (scheControl.SelectedAppointments.Count == 0)
            {
                MessageBox.Show("请至少选择一个工作计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            foreach (DevExpress.XtraScheduler.Appointment app in scheControl.SelectedAppointments)
            {
                DataRowView dr = (DataRowView)schedulerStorage1.Appointments.GetObjectRow(app);
                SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                bool isSucessful = SalesUserSchedulerBs.DeleteSalesUserScheduler(entity);
                if (!isSucessful)
                {
                    MessageBox.Show("删除工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }

            bindScheduler();
        }

        //private void schedulerStorage1_AppointmentInserting(object sender, DevExpress.XtraScheduler.PersistentObjectCancelEventArgs e)
        //{
        //    DevExpress.XtraScheduler.Appointment app = scheControl.SelectedAppointments[0];
        //    DataRowView dr = (DataRowView)schedulerStorage1.Appointments.GetObjectRow(app);

        //    dr["SCHEDULER_ID"] = Guid.NewGuid();
        //    dr["PERSON_ID"] = mSelectedPerson.PersonId;

        //    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);

        //    bool isSucessful = SalesUserSchedulerBs.InsertSalesUserScheduler(entity);
        //    if (!isSucessful)
        //    {
        //        MessageBox.Show("新增工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        return;
        //    }
        //}

        private void schedulerStorage1_AppointmentChanging(object sender, DevExpress.XtraScheduler.PersistentObjectCancelEventArgs e)
        {
            if (scheControl.SelectedAppointments.Count == 0)
            {
                MessageBox.Show("请至少选择一个工作计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            foreach (DevExpress.XtraScheduler.Appointment app in scheControl.SelectedAppointments)
            {
                DataRowView dr = (DataRowView)schedulerStorage1.Appointments.GetObjectRow(app);
                SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                bool isSucessful = SalesUserSchedulerBs.UpdateSalesUserScheduler(entity);
                if (!isSucessful)
                {
                    MessageBox.Show("修改工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            bindScheduler();
        }

        private void schedulerStorage1_AppointmentsInserted(object sender, DevExpress.XtraScheduler.PersistentObjectsEventArgs e)
        {
            schedulerStorage1.Appointments.EndUpdate();

            DataTable dt = schedulerStorage1.Appointments.DataSource as DataTable;
            DataTable dtInsert = dt.Copy();
            dtInsert.Clear();

            foreach (DataRow dr in dt.Rows)
            {
                //if (dr.RowState == DataRowState.Deleted)
                //{
                //    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                //    bool isSucessful=SalesUserSchedulerBs.DeleteSalesUserScheduler(entity);
                //    if (!isSucessful)
                //    {
                //        MessageBox.Show("删除工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //        return;
                //    }
                //}
                if (string.IsNullOrEmpty(dr["SCHEDULER_ID"].ToString()))
                {
                    dr["SCHEDULER_ID"] = Guid.NewGuid();
                    dr["PERSON_ID"] = mSelectedPerson.PersonId;
                    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                    bool isSucessful = SalesUserSchedulerBs.InsertSalesUserScheduler(entity);
                    if (!isSucessful)
                    {
                        MessageBox.Show("新增工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                //else
                //{
                //    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                //    bool isSucessful = SalesUserSchedulerBs.UpdateSalesUserScheduler(entity);
                //    if (!isSucessful)
                //    {
                //        MessageBox.Show("修改工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //        return;
                //    }
                //}
            }
            MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            bindScheduler();
        }

        private void schedulerStorage1_AppointmentsChanged(object sender, DevExpress.XtraScheduler.PersistentObjectsEventArgs e)
        {
            schedulerStorage1.Appointments.EndUpdate();

            DataTable dt = schedulerStorage1.Appointments.DataSource as DataTable;
            DataTable dtInsert = dt.Copy();
            dtInsert.Clear();

            foreach (DataRow dr in dt.Rows)
            {
                //if (dr.RowState == DataRowState.Deleted)
                //{
                //    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                //    bool isSucessful=SalesUserSchedulerBs.DeleteSalesUserScheduler(entity);
                //    if (!isSucessful)
                //    {
                //        MessageBox.Show("删除工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //        return;
                //    }
                //}
                //if (string.IsNullOrEmpty(dr["SCHEDULER_ID"].ToString()))
                //{
                //    dr["SCHEDULER_ID"] = Guid.NewGuid();
                //    dr["PERSON_ID"] = mSelectedPerson.PersonId;
                //    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                //    bool isSucessful = SalesUserSchedulerBs.InsertSalesUserScheduler(entity);
                //    if (!isSucessful)
                //    {
                //        MessageBox.Show("新增工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //        return;
                //    }
                //}
                if (!string.IsNullOrEmpty(dr["SCHEDULER_ID"].ToString()))
                {
                    SalesUserSchedulerEntity entity = new SalesUserSchedulerEntity(dr);
                    bool isSucessful = SalesUserSchedulerBs.UpdateSalesUserScheduler(entity);
                    if (!isSucessful)
                    {
                        MessageBox.Show("修改工作计划失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            bindScheduler();
        }

        
    }
}